//
//  NewFocusNewsCell.m
//  suRead
//
//  Created by lidi on 2018/6/3.
//  Copyright © 2018年 bao. All rights reserved.
//

#import "NewFocusNewsCell.h"
#import "SDCycleScrollView.h"
#import "M_SpecialNews.h"
#import "M_News.h"
#import "NewFocusNewsCellItem.h"

@interface NewFocusNewsCell()<SDCycleScrollViewDelegate>
@property (weak, nonatomic) IBOutlet SDCycleScrollView *cycleScrollView;
@end

@implementation NewFocusNewsCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setNewsDatas:(NSArray *)newsDatas
{
    _newsDatas = newsDatas;
    
    NSInteger focusCount = [newsDatas count];
    if (!focusCount) {
        return;
    }
    
    NSMutableArray *focusNews_ = [NSMutableArray array];
    
    for (int i = 0; i < [_newsDatas count]; i ++) {
        NSString *imageURL;
        if ([[_newsDatas objectAtIndex:i] isKindOfClass:[M_SpecialNews class]]) {
            M_SpecialNews *news = [_newsDatas objectAtIndex:i];
            imageURL = news.f_banner;
        } else {
            M_News *news = [_newsDatas objectAtIndex:i];
            imageURL = news.f_thumbnail;
        }
        [focusNews_ addObject:imageURL];
    }
    self.cycleScrollView.imageURLStringsGroup = focusNews_;
    self.cycleScrollView.autoScrollTimeInterval = 5.0f;
    self.cycleScrollView.pageControlStyle = SDCycleScrollViewPageContolStyleNone;
    self.cycleScrollView.delegate = self;
}

#pragma mark - SDCycleScrollViewDelegate

/** 如果你需要自定义cell样式，请在实现此代理方法返回你的自定义cell的Nib。 */
- (UINib *)customCollectionViewCellNibForCycleScrollView:(SDCycleScrollView *)view {
    return [UINib nibWithNibName:@"NewFocusNewsCellItem" bundle:[NSBundle mainBundle]];
}

/** 如果你自定义了cell样式，请在实现此代理方法为你的cell填充数据以及其它一系列设置 */
- (void)setupCustomCell:(UICollectionViewCell *)cell forIndex:(NSInteger)index cycleScrollView:(SDCycleScrollView *)view {
    NewFocusNewsCellItem *focusCell = (NewFocusNewsCellItem *)cell;
    focusCell.newsData = [self.newsDatas objectAtIndex:index];
}

- (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didSelectItemAtIndex:(NSInteger)index
{
    if (self.focusTapAtIndex) {
        self.focusTapAtIndex([self.newsDatas objectAtIndex:index]);
    }
}

@end
